//
//  BaseViewController.swift
//  SwiftProject
//
//  Created by qindong on 2021/3/3.
//

// MARK: - BaseViewController viewcontroller基类
class BaseViewController: QMUICommonViewController {
    enum NavigationBarType {
        case light
        case custom
        case clear
    }
    var navigationBarType : NavigationBarType = .custom {
        didSet {
            updateNavigationBarAppearance()
        }
    }

    // 用来填充全面屏页面安全区域颜色
    lazy var safeAreaBottomView : UIView = {
        let safeAreaBottomView = UIView(frame: CGRect(x: 0, y: SCREEN_HEIGHT - SafeAreaInsetsConstantForDeviceWithNotch.bottom, width: SCREEN_WIDTH, height: SafeAreaInsetsConstantForDeviceWithNotch.bottom))
        view.addSubview(safeAreaBottomView)
        return safeAreaBottomView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    func requestData() {
        
    }

    lazy var modalViewControllerForAddSubview : QMUIModalPresentationViewController = {
        let modalViewControllerForAddSubview = QMUIModalPresentationViewController()
        modalViewControllerForAddSubview.view.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        return modalViewControllerForAddSubview
    }()
    
    func showCustomViewAddSubview(_ contentView : UIView) {
        modalViewControllerForAddSubview.contentView = contentView
        modalViewControllerForAddSubview.show(in: view, animated: true, completion: nil)
    }
    
    func backBtn() -> UIBarButtonItem {
        .qmui_back(withTarget: self, action: #selector(baseBackBtnAction(btn:)))
    }
    
    func closeBtn() -> UIBarButtonItem {
        .qmui_close(withTarget: self, action: #selector(baseBackBtnAction(btn:)))
    }
    
    func showCustomBackButton(_ isWhite : Bool) {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        let button = QMUIButton(frame: view.bounds)
        button.setImage(UIImage.qmui_image(with: .navBack, size: CGSize(width: 8, height: 14), tintColor: .white)?.withRenderingMode(.alwaysTemplate), for: .normal)
        button.clipsToBounds = true
        button.layer.cornerRadius = 15
        button.addTarget(self, action: #selector(baseBackBtnAction(btn:)), for: .touchUpInside)
        if isWhite {
            button.tintColor = .black
            button.backgroundColor = .white
        } else {
            button.tintColor = .white
            button.backgroundColor = .black.withAlphaComponent(0.3)
        }
        view.addSubview(button)
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: view)
    }

    func showCustomShareButton(_ isWhite : Bool) {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
        let button = QMUIButton(frame: view.bounds)
        button.setImage(UIImage(named: "icon_goods_share")?.withRenderingMode(.alwaysTemplate), for: .normal)
        button.addTarget(self, action: #selector(rightBarItemAction), for: .touchUpInside)
        button.clipsToBounds = true
        button.layer.cornerRadius = 15
        if isWhite {
            button.tintColor = .black
            button.backgroundColor = .white
        } else {
            button.tintColor = .white
            button.backgroundColor = .black.withAlphaComponent(0.3)
        }
        view.addSubview(button)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
    }
    
    func showNormalBackButton(_ color : UIColor) {
        let button = QMUINavigationButton(image: .qmui_image(with: .navBack, size: CGSize(width: 13, height: 21), tintColor: color))
        navigationItem.leftBarButtonItem = UIBarButtonItem.qmui_item(with: button, target: self, action: #selector(baseBackBtnAction(btn:)))
    }

    @objc func rightBarItemAction() {

    }
    
    func showCloseBtn() {
        navigationItem.leftBarButtonItem = backBtn()
    }
    
    func showBackBtn() {
        navigationItem.leftBarButtonItem = closeBtn()
    }
    
    @objc func baseBackBtnAction(btn : UIButton) {
        navigationController?.qmui_popViewController(animated: true, completion: nil)
    }
    
    deinit {
        print("对象被delloc没有造成循环引")
    }
    
    override func didInitialize() {
        super.didInitialize()
    }
    
    //布局统一方法
    override func initSubviews() {
        super.initSubviews()
    }
    
    // 设置导航标题/按钮统一方法
    override func setupNavigationItems() {
        super.setupNavigationItems()
    }
    
    // 设置导航样式
    override func qmui_navigationBarStyle() -> UIBarStyle {
        QMUICMI.navBarStyle
    }
    
    // 导航背景色修改
    override func qmui_navigationBarBackgroundImage() -> UIImage? {
        switch navigationBarType {
        case .custom:
            return .qmui_image(with: .colorMain)!.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1), resizingMode: .stretch)
        case .clear:
            return .qmui_image(with: .clear)!.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1), resizingMode: .stretch)
        case .light:
            return .qmui_image(with: .white)!.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 1, bottom: 0, right: 1), resizingMode: .stretch)
        }
    }
    
    // 设置导航标题颜色
    override func qmui_titleViewTintColor() -> UIColor? {
        switch navigationBarType {
        case .custom:
            return QMUICMI.navBarTitleColor
        case .clear:
            return .white
        case .light:
            return .black
        }
    }
    
    // 设置当前导航栏的 UIBarButtonItem 的 tintColor
    override func qmui_navigationBarTintColor() -> UIColor? {
        switch navigationBarType {
        case .custom:
            return QMUICMI.navBarTintColor
        case .clear:
            return .white
        case .light:
            return .black
        }
    }
    
    // 设置当前导航栏的 barTintColor
    override func qmui_navigationBarBarTintColor() -> UIColor? {
//        switch navigationBarType {
//        case .custom:
//            return QMUICMI.navBarBarTintColor
//        case .clear:
//            return .clear
//        case .light:
//            return .white
//        }
        .clear
    }
    
    // 使用导航切换不同样式的导航时 请设置不同的key使转场顺滑
    override func customNavigationBarTransitionKey() -> String? {
        "base"
    }
    
    // 是否隐藏导航
    override func preferredNavigationBarHidden() -> Bool {
        false
    }
    
    // 是否强制开启手势返回
    override func forceEnableInteractivePopGestureRecognizer() -> Bool {
        true
    }
    
    // 当转场前后界面导航显隐不一时 设置接管显隐
    override func shouldCustomizeNavigationBarTransitionIfHideable() -> Bool {
        true
    }
    
    // 当系统字体变化时的通知
    override func contentSizeCategoryDidChanged(_ notification: Notification) {
        super.contentSizeCategoryDidChanged(notification)
    }
    
    // 设置状态栏颜色
    override var preferredStatusBarStyle: UIStatusBarStyle {
        switch navigationBarType {
        case .custom:
            return .lightContent
        case .clear:
            return .lightContent
        case .light:
            return .default
        }
    }
    
    // 是否支持屏幕旋转
    override var shouldAutorotate: Bool {
        false
    }
    
    // 支持屏幕旋转的方向
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        .portrait
    }
    
    // 点击view自动隐藏键盘
    override func shouldHideKeyboardWhenTouch(in view: UIView?) -> Bool {
        true
    }
}

//MARK: - QMUIKit UI布局统一管理 单独ViewController可重写自定义 具体请点击方法查看详情备注
extension BaseViewController {

}

